package com.ruoyi.oos.web.controller.overrun;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.PermissionUtils;
import com.ruoyi.oos.self.domain.OverrunRecordSelf;
import com.ruoyi.oos.self.domain.TagConfigSelf;
import com.ruoyi.oos.self.domain.TagLimitdtlSelf;
import com.ruoyi.oos.self.mapper.TagLimitdtlSelfMapper;
import com.ruoyi.oos.self.service.IOverrunRecordSelfService;
import com.ruoyi.oos.self.service.ITagConfigSelfService;
import com.ruoyi.oos.tag.domain.TagConfig;
import com.ruoyi.oos.tag.domain.TagLimitdtl;
import com.ruoyi.oos.tag.service.ITagConfigService;
import com.ruoyi.oos.tag.service.impl.PiUtilBean;
import com.ruoyi.web.controller.tag.ChartBean;

/**
 * 超限记录Controller
 * 
 * @author wzy1
 * @date 2020-07-06
 */
@Controller
@RequestMapping("/self/overRecordself")
public class OverrunRecordSelfController extends BaseController
{
    private String prefix = "self/overRecordself";

    @Autowired
    private IOverrunRecordSelfService overrunRecordSelfService;
    
    @Autowired
    private  ITagConfigSelfService tagConfigService;
    
    @Autowired
    private  ITagConfigService configService;
    
    @Autowired
    private TagLimitdtlSelfMapper limitMapper;
    
    
    
    
    

    @GetMapping()
    public String self()
    {
        return prefix + "/self";
    }

        /**
     * 查询超限记录列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OverrunRecordSelf overrunRecordSelf)
    {
        startPage();
        long userid = Long.valueOf(String.valueOf(PermissionUtils.getPrincipalProperty("userId")));
        overrunRecordSelf.setUserid(userid);
        List<OverrunRecordSelf> list = overrunRecordSelfService.selectOverrunRecordSelfList(overrunRecordSelf);
        return getDataTable(list);
    }
    
    /**002152
     * 导出超限记录列表
     */
    @Log(title = "超限记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OverrunRecordSelf overrunRecordSelf)
    {
        List<OverrunRecordSelf> list = overrunRecordSelfService.selectOverrunRecordSelfList(overrunRecordSelf);
        ExcelUtil<OverrunRecordSelf> util = new ExcelUtil<OverrunRecordSelf>(OverrunRecordSelf.class);
        return util.exportExcel(list, "self");
    }

    /**
     * 新增超限记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存超限记录
     */
    @Log(title = "超限记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OverrunRecordSelf overrunRecordSelf)
    {
    	 long userid = Long.valueOf(String.valueOf(PermissionUtils.getPrincipalProperty("userId")));
         overrunRecordSelf.setUserid(userid);
        return toAjax(overrunRecordSelfService.insertOverrunRecordSelf(overrunRecordSelf));
    }

    /**
     * 修改超限记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OverrunRecordSelf overrunRecordSelf = overrunRecordSelfService.selectOverrunRecordSelfById(id);
        mmap.put("overrunRecordSelf", overrunRecordSelf);
        return prefix + "/edit";
    }

    /**
     * 修改保存超限记录
     */
    @Log(title = "超限记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OverrunRecordSelf overrunRecordSelf)
    {
        return toAjax(overrunRecordSelfService.updateOverrunRecordSelf(overrunRecordSelf));
    }

    /**
     * 删除超限记录
     */
    @Log(title = "超限记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(overrunRecordSelfService.deleteOverrunRecordSelfByIds(ids));
    }
    
 
   @GetMapping("/echart/{id}")
   public String echart(@PathVariable("id") Long id, ModelMap map) throws Exception
   {
   	OverrunRecordSelf record = overrunRecordSelfService.selectOverrunRecordSelfById(id);//查询超限记录
   	String starttime = DateUtils.addDateStr(record.getStarttime(), Calendar.MINUTE, -10);
   	String endtime = DateUtils.addDateStr(record.getEndtime(), Calendar.MINUTE, 10);
   	TagConfigSelf tag = tagConfigService.selectTagConfigSelfById(record.getTagid());//查询测点
   	PiUtilBean brean = configService.getChartData(tag.getPitagname(), starttime, endtime);//查询测点曲线数据
     List<Float> list =brean.getRetrunList();
     map.put("tagConfig", tag.getTagname()+"("+DateUtils.dateTime1(record.getStarttime())+"至"+DateUtils.dateTime1(record.getEndtime())+")");
     map.put("list", list);
     map.put("xList",brean.getReturntimes());
       
       TagLimitdtlSelf limtitl = new TagLimitdtlSelf();
       limtitl.setConfigid(tag.getId());
      List<TagLimitdtlSelf> limittlList =  limitMapper.selectTagLimitdtlSelfList(limtitl);
       Float i = 2f;
      if(limittlList!= null && list.size()>0) {
   	   List<ChartBean> limitList = new ArrayList<ChartBean>();
   	   for (TagLimitdtlSelf tagLimitdtl : limittlList) {
   		   ChartBean bean = new ChartBean();
   		   Float limitvalue=  tagLimitdtl.getLimitval();
   		   if(limitvalue == null) {
   			   limitvalue =0f;
   		   }
   		   bean.setyAxis(limitvalue);
   		   limitList.add(bean);
   	   }
   	   map.put("limitList", limitList); 
      }
      
      return prefix + "/echartData";
   }
   
    
    
    
}
